package com.gastudio.SMSimple;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

/**
 * Created by 余艾龙 on 2017/6/21.
 */
public class NormalEncoder extends ProtocolEncoderAdapter {
    private final Charset charset;

    public NormalEncoder(Charset charset) {
        this.charset = charset;
    }


    @Override
    public void encode(IoSession session, Object in, ProtocolEncoderOutput output) throws Exception {
        ProtocolPack pack=(ProtocolPack) in;
        CharsetEncoder ce=charset.newEncoder();
        IoBuffer buf=IoBuffer.allocate(100).setAutoExpand(true);
        String statusLine="M sip:wap.baidu.com.cn SIP-C/2.0";
        String sender=pack.getSMS().getSender();
        String receiver=pack.getSMS().getReceiver();
        String smsContent=pack.getSMS().getMessage();
        buf.putInt(pack.getLength());
        buf.put(pack.getFlag());
        if(pack.getSMS()!=null) {
            buf.putObject(pack.getSMS());
        }
//        buf.putString(statusLine+"/n",ce);
//        buf.putString("发送号码:"+sender+"/n",ce);
//        buf.putString("接收号码:"+sender+"/n",ce);
//        buf.putString("短信内容长度:"+(smsContent.getBytes(charset).length)+"/n",ce);
//        buf.putString(smsContent,ce);
        buf.flip();
        output.write(buf);
    }


}
